package com.wzl.common;

import com.wzl.common.constant.DpPublicConstants;
import com.wzl.common.errcode.DpErrorCode;
import com.wzl.common.errcode.DpErrorCodeEnum;
import lombok.Data;

import java.io.Serializable;
import java.util.Map;

/**
 * 接口返回类基类
 *
 * @author wzl
 * @since 2022-08-27 15:40
 */
@Data
public class DpPublicResponse implements Serializable {

    private static final long serialVersionUID = 1865981403129616196L;
    /**
     * 错误码
     */
    private String errorCode;
    /**
     * 错误描述
     */
    private String errorMsg;
    /**
     * 返回数据
     */
    private Object data;

    /**
     * 为后续业务发展需要，增加响应协议扩展字段
     */
    private Map<String, String> expandField;


    /**
     * 构造函数，设置默认值
     */
    public DpPublicResponse() {
        errorCode = DpPublicConstants.SUCCESS_CODE;
        errorMsg = DpPublicConstants.SUCCESS_MESSAGE;
    }

    public DpPublicResponse(DpErrorCodeEnum dpErrorCodeEnum) {
        this.errorCode = dpErrorCodeEnum.getCode();
        this.errorMsg = dpErrorCodeEnum.getMessage();
    }

    public DpPublicResponse(DpErrorCodeEnum dpErrorCodeEnum, Object data) {
        this.errorCode = dpErrorCodeEnum.getCode();
        this.errorMsg = dpErrorCodeEnum.getMessage();
        this.data = data;
    }

    public DpPublicResponse(String errorCode, String errorMsg) {
        this.errorCode = errorCode;;
        this.errorMsg = errorMsg;
    }

    public DpPublicResponse(String errorCode, String errorMsg, Object data) {
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
        this.data = data;
    }


    public void setErrorInfo(DpErrorCode info) {
        if(info == null) {
            return;
        }
        this.errorCode = info.getCode();
        this.errorMsg = info.getMessage();
    }

    public void setErrorInfo(DpPublicException e) {
        if( e == null) {
            return;
        }
        this.errorCode = e.getErrCode();
        this.errorMsg = e.getErrMsg();
    }
}
